package data

import (
	"gitchina/CPTT4WeChat/util"

	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

var connStr string

const (
	Add_Check_In_Stats                  = `INSERT INTO check_in_stats (uid, garden_id, owner_id, group_id, category_name,category_type,data,data_type,data_format,signature) SELECT ?,(SELECT id FROM garden WHERE uid=?) AS garden_id, (SELECT id FROM user_bind WHERE uid=?) AS owner_id, ?, ?,?,?,?,?,?`
	Get_Check_In_Stats_Signature        = `SELECT signature FROM check_in_stats WHERE uid = ?`
	Update_Check_In_Stats               = `UPDATE check_in_stats SET data = ?, signature = ? WHERE uid = ? AND signature = ?`
	Get_Check_In_Data_Signature         = `SELECT signature FROM check_in_data WHERE uid = ?`
	Add_Check_In_Data                   = `INSERT INTO check_in_data (uid, garden_id, owner_id, user_no, user_name ,data,data_type,data_format,signature) SELECT ?,(SELECT id FROM garden WHERE uid=?) AS garden_id, (SELECT id FROM user_bind WHERE uid=?) AS owner_id, ?,?,?,?,?,?`
	Update_Check_In_Data                = `UPDATE check_in_data SET data = ?, signature = ? WHERE uid = ? AND signature = ?`
	Get_Last_UpdTime_From_Check_In_Data = `SELECT MAX(update_time) FROM check_in_data WHERE garden_id = ? AND data_type = ?`
)

func InitOrm(runmode string) {
	if runmode != "prod" {
		orm.Debug = true
		if runmode == "uat" {
			connStr = "uat:@WW%_)2a@tcp(121.199.0.64:22479)/CPTT4WeChatDB?charset=utf8"
		} else if runmode == "test" {
			connStr = "test:123456@tcp(120.26.223.135:22479)/testdb?charset=utf8"
		} else {
			connStr = "root:123456@tcp(127.0.0.1:3306)/CPTT4WeChatDB?charset=utf8"
		}
	}

	orm.DebugLog = orm.NewLog(util.UseBeeLogger())
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", connStr)
}

func GetConnStr() string {
	return connStr
}
